<?php

namespace App\Console\Commands\Export;

use App\Kuafu\CouponExt\Enum\CouponExtEnum;
use App\Kuafu\Order\Enum\CodeEnum;
use App\Kuafu\Order\Model\OrderCouponSplitModel;
use App\Kuafu\Order\Model\OrderDetailSplitModel;
use App\Kuafu\Order\Model\OrderDiscountSplitModel;
use App\Kuafu\Order\Model\OrderSplitModel;
use App\Kuafu\Order\Service\OrderTable;
use App\Kuafu\Pay\BasePay;
use App\Kuafu\Pay\Channel\FuYou\FuYouBase;
use App\Kuafu\Pay\Service\PayService;
use App\Kuafu\Shop\Model\Shop;
use App\Kuafu\Shop\Service\ShopCameraSyncService;
use App\Kuafu\Shop\Service\ShopService;
use App\Kuafu\WaiMai\Enum\WaiMaiDBEnum;
use App\Kuafu\WaiMai\Model\WaiMaiShopMappingModel;
use App\Library\FeiShu\SendFs;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Facades\Excel;

class ExportDyDataCommand extends Command
{

    protected $signature = 'export:dyData';

    protected $description = '抖音数据导出';

    public function handle()
    {
        // 循环 100 张表 获取 查询订单列表
        $startTimeAt = strtotime('2023-08-18');
        $endTimeAt   = strtotime('2023-08-24');

        $list = [
            [
                '门店名称',
                '抖音套餐券核销总数',
                '抖音小程序渠道核销抖音套餐券核销'
            ]
        ];

        $raw = [];
        for ($i = 0; $i <= 99; $i++) {
            // 主表数据
            $orderCouponExtTableName = OrderTable::orderCouponExtTableName($i);
            $orderTableName          = OrderTable::orderTableName($i);


            // 总数
            $countAll = OrderSplitModel::suffix($i)
                                       ->query()
                                       ->selectRaw('count(*) as count,kf_' . $orderTableName . '.shopId')
                                       ->whereBetween('completeTime', [$startTimeAt, $endTimeAt])
                                       ->leftJoin($orderCouponExtTableName, $orderCouponExtTableName . '.orderId', '=',
                                           $orderTableName . '.orderId')
                                       ->where($orderCouponExtTableName . '.extPlatform',
                                           CouponExtEnum::EXT_PLATFORM_DOUYIN)
                                       ->with([
                                           'orderCouponExtRel' => function ($query) {
                                               $query->where('couponExtStatus',
                                                   CouponExtEnum::COUPON_STATUS_USED);
                                           }
                                       ])
                                       ->groupBy($orderTableName . '.shopId')
                                       ->get()
                                       ->toArray();

            $countAll = array_column($countAll, null, 'shopId');

            $countDy = OrderSplitModel::suffix($i)
                                      ->query()
                                      ->where('sourceType', 6)
                                      ->selectRaw('count(*) as count,kf_' . $orderTableName . '.shopId')
                                      ->whereBetween('completeTime', [$startTimeAt, $endTimeAt])
                                      ->leftJoin($orderCouponExtTableName, $orderCouponExtTableName . '.orderId', '=',
                                          $orderTableName . '.orderId')
                                      ->where($orderCouponExtTableName . '.extPlatform',
                                          CouponExtEnum::EXT_PLATFORM_DOUYIN)
                                      ->with([
                                          'orderCouponExtRel' => function ($query) {
                                              $query->where('couponExtStatus',
                                                  CouponExtEnum::COUPON_STATUS_USED);
                                          }
                                      ])
                                      ->groupBy($orderTableName . '.shopId')
                                      ->get()
                                      ->toArray();

            $countDy = array_column($countDy, null, 'shopId');


            foreach ($countAll as $key => $item) {
                $raw[] = [
                    ShopService::getShopInfo($item['shopId'])['name'],
                    $item['count'],
                    $countDy[$key]['count'] ?? '0'
                ];
            }
        }
        $list = array_merge($list, $raw);


        $csvStr = '';
        foreach ($list as $item) {
            $csvStr .= implode(',', $item) . "\r\n";
        }

        file_put_contents('./tmp/抖音核销数据.csv', $csvStr);
    }
}
